package com.example.controller;

import com.example.service.RocketMQProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rocketmq")
public class RocketMQController {

    @Autowired
    private RocketMQProducerService producerService;

    /**
     * 发送消息到指定的topic，如果topic为空则使用默认的topic
     *
     * @param topic   topic名称，如果为空则使用默认的topic
     * @param message 消息内容
     * @return 返回结果
     */
    @PostMapping("/send")
    public String sendMessage(@RequestParam(required = false) String topic, @RequestParam String message) {
        producerService.sendMessage(topic, message);
        return "Message sent to topic " + (topic == null ? "default" : topic) + ": " + message;
    }

    /**
     * 发送消息到配置的default-topic
     *
     * @param message 消息内容
     * @return 返回结果
     */
    @PostMapping("/send/default")
    public String sendMessageToDefaultTopic(@RequestParam String message) {
        producerService.sendMessageToDefaultTopic(message);
        return "Message sent to default topic: " + message;
    }
}
